package com.h3c.nem.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;

import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author chenyifan
 * @date 2022/6/6 13:39
 * 学习形式枚举类
 */
@Getter
public enum StudyTypeEnum {

    FULL_TIME("01", "全日制统招", "全国普通高等院校全日制"),

    ON_JOB_TRAINING("02","在职培训", ""),

    CORR_COURSES("03","函授课程", ""),

    TV_UNIVERSITY("04","电视大学", ""),

    OTHERS("05","其他", "其它"),

    UN_FULL_TIME("06","非全日制统招", "全国普通高等院校非全日制"),

    ADULT_EDU("07","成人高等教育", "成人高等教育"),

    PRO_UPGRADE("08","统招专升本", "统招专升本"),

    SELF_TEST("09","自学考试", "自学考试"),

    UN_PRO_UPGRADE("10","非统招专升本", "非统招专升本"),

    OVERSEAS_STUDENT("11","海外留学生", "海外留学生");

    @EnumValue
    @JsonValue
    private final String code;

    private final String typeName;

    private final String beiSenName;

    StudyTypeEnum(String code, String typeName, String beiSenName) {
        this.code = code;
        this.typeName = typeName;
        this.beiSenName = beiSenName;
    }

    /**
     * code 作为 key，封装为 Map
     */
    private static final Map<String, StudyTypeEnum> CODE_MAP = Stream
            .of(StudyTypeEnum.values())
            .collect(Collectors.toMap(StudyTypeEnum::getCode, Function.identity()));


    /**
     * 静态工厂反序列化
     *
     * @param key code
     * @return 评论的类型枚举
     */
    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static StudyTypeEnum valueOfKey(String key) {
        return Optional.ofNullable(CODE_MAP.get(key))
                .orElseThrow(() -> new IllegalArgumentException(String.valueOf(key)));
    }

    public static StudyTypeEnum getEnumByBeiSenName(String beiSenName) {
        for (StudyTypeEnum value : StudyTypeEnum.values()) {
            if (beiSenName.equals(value.beiSenName)) {
                return value;
            }
        }
        return OTHERS;
    }
}
